package com.dzf.service.impl;

import com.dzf.entity.Hot;
import com.dzf.entity.LargeFile;
import com.dzf.mapper.HotMapper;
import com.dzf.mapper.LargeFileMapper;
import com.dzf.service.HotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class HotServiceImpl implements HotService {
    @Autowired
    HotMapper hotMapper;
    @Autowired
    LargeFileMapper largeFileMapper;

    @Transactional
    @Override
    public Hot addHot(Hot hot, MultipartFile pic) {
        UUID uuid = UUID.randomUUID();
        LargeFile largeFile = new LargeFile();
        try {
            if (pic!=null&&pic.getSize()>0)
            {
                largeFile.setId(uuid.toString());
                largeFile.setFilename(pic.getOriginalFilename());
                largeFile.setContent(pic.getBytes());
            }
            largeFileMapper.addLargeFile(largeFile);//文件内容
            if (largeFile.getId()!=null ||" ".equals(largeFile.getId()))
            {
                hot.setPicture(uuid.toString());
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }

        hotMapper.addHot(hot);//先添加热点图
        Hot hot1 = hotMapper.findById(hot.getId());//利用添加后返回的id ，把新添加的对象查询出来返回出去
        return hot1;
    }

    @Override
    public List<Hot> findAll() {
        return hotMapper.findAll();
    }

    @Override
    public Hot findById(int id) {
        return hotMapper.findById(id);
    }

    @Override
    public void delete(int id) {
        hotMapper.delete(id);
    }

    @Override
    public List<Hot> findHot(Map<String, Object> map) {
        return hotMapper.findHot(map);
    }
}
